package bidsanity



import grails.test.mixin.*
import org.junit.*
import GrailsErrors.ErrorCodes

/**
 * Unit tests for the Customer domain class.
 */
@TestFor(Customer)
class CustomerTests {

    void testAdd() {
        def c = new Customer(emailAddress:  'test@example.org', password: "testing")
        c.save(failOnError: true)
        Assert.assertFalse c.hasErrors()

        Assert.assertEquals Customer.list().size(), 1
        Assert.assertNotNull c.emailAddress
        Assert.assertNotNull c.password
    }

    void testNotNullFields() {
        def c = new Customer(emailAddress:  null, password: null)
        c.save(failOnError:false)
        Assert.assertTrue ErrorCodes.CheckErrorCode(c, 'emailAddress', ErrorCodes.NULLABLE)
        Assert.assertTrue ErrorCodes.CheckErrorCode(c, 'password', ErrorCodes.NULLABLE)
    }

    void testDelete() {
        def c = new Customer(emailAddress:  'test@example.org', password: "testing")
        c.save(failOnError: true)
        Assert.assertFalse c.hasErrors()              
        Assert.assertEquals Customer.list().size(), 1
        
        c.delete()
        Assert.assertFalse c.hasErrors()
        Assert.assertEquals Customer.list().size(), 0


    }
    
    void testInvalidEmail() {
        def c = new Customer(emailAddress:  'test@.org', password: "testing")
        c.save(failOnError: false)
        Assert.assertTrue c.hasErrors()

        Assert.assertEquals Customer.list().size(), 0
        Assert.assertTrue ErrorCodes.CheckErrorCode(c, 'emailAddress', ErrorCodes.MATCHES_INVALID)
    }

    void testTooShortPassword() {
        def c = new Customer(emailAddress:  'test@example.org', password: "test")
        c.save()
        Assert.assertTrue c.hasErrors()

        Assert.assertEquals Customer.list().size(), 0
        Assert.assertTrue ErrorCodes.CheckErrorCode(c, 'password', ErrorCodes.TOO_SMALL)
    }

    void testTooLongPassword() {
        def c = new Customer(emailAddress:  'test@example.org', password: "testing23")
        c.save()
        Assert.assertTrue c.hasErrors()

        Assert.assertEquals Customer.list().size(), 0
        Assert.assertTrue ErrorCodes.CheckErrorCode(c, 'password', ErrorCodes.TOO_BIG)
    }
}

